<?php
namespace app\admin\controller;

use think\facade\Session;

class SessionTest extends \app\BaseController
{
    /**
     * 设置session
     */
    public function set()
    {
        // 设置session保存路径
        $sessionPath = runtime_path() . 'session';
        if (!is_dir($sessionPath)) {
            mkdir($sessionPath, 0755, true);
        }
        session_save_path($sessionPath);
        
        // 启动session
        if (!session_id()) {
            session_start();
        }
        
        // 设置原生session
        $_SESSION['test_data'] = [
            'time' => time(),
            'message' => 'Hello Session'
        ];
        
        // 同时设置ThinkPHP session
        Session::set('test_data', [
            'time' => time(),
            'message' => 'Hello Session'
        ]);
        
        return json([
            'code' => 1,
            'msg' => 'Session设置成功',
            'session_id' => session_id(),
            'native_data' => $_SESSION['test_data'] ?? null,
            'think_data' => Session::get('test_data')
        ]);
    }
    
    /**
     * 获取session
     */
    public function get()
    {
        // 设置session保存路径
        $sessionPath = runtime_path() . 'session';
        session_save_path($sessionPath);
        
        // 启动session
        if (!session_id()) {
            session_start();
        }
        
        $nativeData = $_SESSION['test_data'] ?? null;
        $thinkData = Session::get('test_data');
        
        return json([
            'code' => 1,
            'msg' => 'Session获取完成',
            'session_id' => session_id(),
            'has_native_data' => isset($_SESSION['test_data']),
            'has_think_data' => Session::has('test_data'),
            'native_data' => $nativeData,
            'think_data' => $thinkData
        ]);
    }
}
